package top.maof.book.spider.develop.contents.bixia;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.spider.develop.Split;
import top.maof.book.utils.URLUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.util.Date;

/**
 * 新笔下文学
 *
 * @author 毛逢
 * @date 2021/5/24 15:36
 */
@Data
@TargetUrl("https://www.newbixia.cc/book/\\d+/\\d+/")
@HelpUrl(value = {"https://www.newbixia.cc/xuanhuan/\\d+.html", "https://www.newbixia.cc/xiuzhen/\\d+.html",
        "https://www.newbixia.cc/dushi/\\d+.html", "https://www.newbixia.cc/lishi/\\d+.html",
        "https://www.newbixia.cc/wangyou/\\d+.html", "https://www.newbixia.cc/kehuan/\\d+.html",
        "https://www.newbixia.cc/yanqing/\\d+.html", "https://www.newbixia.cc/qita/\\d+.html",
        "https://www.newbixia.cc/paihangbang/", "https://www.newbixia.cc/quanben/"})
@Slf4j
public class NewBiXia implements AfterExtractor, Split {
    @ExtractBy(value = "//div[@id=maininfo]/div[@id=info]/h1/text()")
    private String name;

    @ExtractBy(value = "//div[@id=maininfo]/div[@id=info]/p[1]/text()")
    private String author;//作者:伊然淳静

    @ExtractBy(value = "//div[@id=fmimg]/img/@src")
    private String img;

    @ExtractBy(value = "//div[@class=con_top]/a[2]/text()")
    private String classify;//科幻小说

    private Date updateDate;

    private Date insertDate = new Date();

    @ExtractBy(value = "//div[@id=intro]/text()")
    private String synopsis;

    private String state;

    private String url;

    private String content;

    private Integer webId = 11;

    public static final String CHARSET = "utf-8";

    @Override
    public Book getBook() {
        Book book = new Book();
        book.setAuthor(this.author);
        book.setState(this.state);
        book.setImg(this.img);
        book.setName(this.name);
        book.setInsertDate(this.insertDate);
        book.setClassify(this.classify);
        book.setSynopsis(this.synopsis);
        book.setUpdateDate(this.updateDate);
        return book;
    }

    @Override
    public Contents getContents() {
        Contents contents = new Contents();
        contents.setUrl(this.url);
        contents.setContent(this.content);
        contents.setWebId(this.webId);
        contents.setUpdateDate(this.updateDate);
        return contents;
    }


    @Override
    public void afterProcess(Page page) {
        //获取最新章节更新时间   最后更新：2019-04-17 02:11:06
        String updateStr = page.getHtml().xpath("//div[@id=info]/p[3]/text()").toString().substring(7, 17).trim();


        //对得到的作者字段做处理  作者：唐家三少
        try {
            this.author = this.author.split("：")[1];
        } catch (Exception e) {
            log.info("作者获取错误..." + e.getMessage());
        }


        //获取该页面url
        String url = page.getRequest().getUrl();
        this.setUrl(url);


        try {
            this.img = URLUtils.completeUrl(this.url, this.img);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

    }
}
